package com.td.filemanager.business.filethumbnail;

import com.td.filemanager.ResourceTable;
import com.td.filemanager.business.filedetail.FileDetailAbilitySlice;
import com.td.filemanager.business.filetype.FileTypeViewModel;
import com.td.filemanager.provider.ListComponentProvider;
import com.td.filemanager.slice.BaseAbilitySlice;
import com.td.filemanager.ui.widget.ActionBar;
import com.td.lib.viewmodel.DefaultDataObserver;
import com.td.lib.viewmodel.ViewModelAbility;
import com.td.lib.viewmodel.ViewModelProvider;
import com.td.library.config.Constant;
import com.td.library.config.FileType;
import com.td.library.data.FileData;
import com.td.library.media.audio.AudioAbilitySlice;
import com.td.library.media.video.VideoAbilitySlice;
import com.td.library.utils.LogUtil;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

import java.util.ArrayList;
import java.util.List;


/**
 * 文件缩略图页面
 */
public class FileThumbnailAbilitySlice extends BaseAbilitySlice {
    private static final String TAG = FileThumbnailAbilitySlice.class.getSimpleName();

    private ListContainer mListContainer;

    private ListContainer mFilterContainer;

    private ActionBar mActionBar;

    private Checkbox mCheckBox;

    private Component mCoverComponent;

    private List<String> fileTypeNames;

    private FileThumbnailDataProvider mFileDataProvider;

    private FileThumbnailFilterProvider mFileFilterProvider;

    private FileThumbnailViewModel mViewModel;


    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_slice_file_thumbnail);
        initComponent();
        initViewModel();
        initListener();
        initObserver();
    }

    @Override
    public void onActive() {
        super.onActive();
        setFilterToDefalut();
    }

    private void initComponent() {
        mCheckBox = (Checkbox) findComponentById(ResourceTable.Id_selectedFileTypeText);
        mCoverComponent = findComponentById(ResourceTable.Id_cover);
        mListContainer = (ListContainer) findComponentById(ResourceTable.Id_list_container);
        mFilterContainer = (ListContainer) findComponentById(ResourceTable.Id_filter);
        mActionBar = (ActionBar) findComponentById(ResourceTable.Id_actionBar);
        mActionBar.setLeftClickListener(component -> {
            terminate();
        });
        fileTypeNames = new ArrayList<>();
        FileType[] fileTypes = FileType.values();
        for (int i = 0; i < fileTypes.length; i++) {
            fileTypeNames.add(fileTypes[i].getTypeName());
        }
        initFilterProvider(fileTypeNames);
        mCheckBox.setText(fileTypeNames.get(0));
        mCheckBox.setCheckedStateChangedListener((component, isChecked) -> {
            LogUtil.warn(TAG, "isChecked" + isChecked);
            // 状态改变的逻辑
            if (isChecked) {
                mFilterContainer.setVisibility(Component.VISIBLE);
                mCoverComponent.setVisibility(Component.VISIBLE);
            } else {
                mFilterContainer.setVisibility(Component.HIDE);
                mCoverComponent.setVisibility(Component.HIDE);
            }
        });
        mCoverComponent.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                mFilterContainer.setVisibility(Component.HIDE);
                mCoverComponent.setVisibility(Component.HIDE);
                mCheckBox.setChecked(false);
            }
        });
    }

    private void initListener() {
    }

    private void initViewModel() {
        if (getAbility() instanceof ViewModelAbility) {
            ViewModelAbility viewModelAbility = (ViewModelAbility) getAbility();
            mViewModel = new ViewModelProvider(viewModelAbility.getViewModelStore())
                    .get(FileThumbnailViewModel.class);
            FileTypeViewModel fileTypeViewModel =
                    new ViewModelProvider(viewModelAbility.getViewModelStore()).get(FileTypeViewModel.class);
            mViewModel.setSelectDeviceData(fileTypeViewModel.getSelectedDevices());
            mViewModel.setParentFilePath(viewModelAbility.getDistributedDir().getPath());
            mViewModel.setSelectFileType(fileTypeViewModel.getSelectedFileTypes());
            mViewModel.refresh();
        }
    }

    private void initObserver() {
        mViewModel.getFileData().addObserver(
                new DefaultDataObserver<List<FileData>>(getLifecycle()) {
                    @Override
                    public void onChanged(List<FileData> fileData) {
                        LogUtil.info(TAG, "onChanged getDevicedata " + fileData.size());
                        if (mFileDataProvider == null) {
                            initDeviceDataProvider(fileData);
                        } else {
                            mFileDataProvider.notifyDataChanged();
                        }
                    }
                }, false);
        mViewModel.getSelectedTypeFiles().addObserver(
            new DefaultDataObserver<List<FileData>>(getLifecycle()) {
                @Override
                public void onChanged(List<FileData> fileData) {
                    LogUtil.info(TAG, "onChanged getSelectedTypeFiles " + fileData.size());
                    initDeviceDataProvider(fileData);
                }
            }, false);
    }

    private void initDeviceDataProvider(List<FileData> fileData) {
        mFileDataProvider = new FileThumbnailDataProvider(FileThumbnailAbilitySlice.this,
                fileData, ResourceTable.Layout_file_info_item);
        mListContainer.setItemProvider(mFileDataProvider);
        mFileDataProvider.setOnItemClickListener(new ListComponentProvider.OnItemClickListener<FileData>() {
            @Override
            public void onItemClick(Component component, FileData item, int position) {
                LogUtil.info(TAG, "onItemClick " + position + " item " + item);
                mViewModel.setSelectFilePos(position);
                if (mCheckBox != null) {
                    mCheckBox.setChecked(false);
                }
                Intent intent = new Intent();
                switch (item.getFileType()) {
                    case VIDEO:
                        intent.setParam(Constant.INTENT_STR_FILE_PATH, item.getAbsolutePath());
                        present(new VideoAbilitySlice(), intent);
                        break;
                    case AUDIO:
                        intent.setParam(Constant.INTENT_STR_FILE_PATH, item.getAbsolutePath());
                        present(new AudioAbilitySlice(), intent);
                        break;
                    default:
                        present(new FileDetailAbilitySlice(), intent);
                        break;
                }
            }
        });
        mFileDataProvider.notifyDataChanged();
    }

    private void initFilterProvider(List<String> fileTypeName) {
        mFileFilterProvider = new FileThumbnailFilterProvider(FileThumbnailAbilitySlice.this,
            fileTypeName, ResourceTable.Layout_file_filter_item);
        mFilterContainer.setItemProvider(mFileFilterProvider);
        mFileFilterProvider.setOnItemClickListener(new ListComponentProvider.OnItemClickListener<String>() {
            @Override
            public void onItemClick(Component component, String item, int position) {
                LogUtil.info(TAG, "onItemClick " + position + " item " + item);
                if (mCheckBox != null) {
                    mCheckBox.setText(item);
                    mCheckBox.setChecked(false);
                }
                mViewModel.filterFileByType(item);
            }
        });
    }

    private void setFilterToDefalut() {
        if (mCheckBox != null) {
            mCheckBox.setText(FileType.FILE.getTypeName());
            mCheckBox.setChecked(false);
        }
        if (mViewModel.getFileData().getData() != null && mViewModel.getFileData().getData().size() > 0) {
            mViewModel.filterFileByType(FileType.FILE.getTypeName());
        }
    }
}
